const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();
myEmitter.on('event', function(a, b) {
  console.log(a, b, this);
  // Prints:
  //   a b MyEmitter {
  //     domain: null,
  //     _events: { event: [Function] },
  //     _eventsCount: 1,
  //     _maxListeners: undefined }
});
myEmitter.emit('event', 'a', 'b');

const myEmitter2 = new MyEmitter();
myEmitter2.on('event', (a, b) => {
  console.log(a, b, this);
  // Prints: a b {}
});
myEmitter2.emit('event', 'a', 'b');

const myEmitter3 = new MyEmitter();
myEmitter3.on('event', (a, b) => {
  setImmediate(() => {
    console.log('this happens asynchronously');
  });
});
myEmitter3.emit('event', 'a', 'b');

const myEmitter4 = new MyEmitter();
var m = 0;
myEmitter4.once('event', () => {
  console.log(++m);
});
myEmitter4.emit('event');
// Prints: 1
myEmitter4.emit('event');
// Ignored4